<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业法人" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入企业法人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="企业联系人" prop="enterpriseContact">
        <el-input
          v-model="queryParams.enterpriseContact"
          placeholder="请输入企业联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业类型" prop="enterpriseType">
        <el-select
          v-model="queryParams.enterpriseType"
          placeholder="请选择企业类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in enterpriseTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="addApart"
          v-hasPermi="['energy:qyEnterprise:edit']"
        >公寓租赁
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="addCom"
          v-hasPermi="['energy:qyEnterprise:remove']"
        >企业增租
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:qyEnterprise:export']"
        >导出
        </el-button
        >
      </el-col>




      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="qyEnterpriseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="企业名称" align="center" prop="enterpriseName"/>
      <el-table-column label="企业法人" align="center" prop="legalPerson"/>
      <el-table-column label="法人联系电话" align="center" prop="legalPhone"/>
      <el-table-column
        label="企业联系人"
        align="center"
        prop="enterpriseContact"
      />
      <el-table-column
        label="企业联系人电话"
        align="center"
        prop="contactPhone"
      />
      <el-table-column
        label="企业类型"
        align="center"
        prop="enterpriseType"
        :formatter="enterpriseTypeFormat"
      />
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:qyEnterprise:edit']"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:qyEnterprise:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!--<el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改企业概况对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        size="small"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="企业名称" prop="customerId">
          <el-select
            clearable
            v-model="form.customerId"
            placeholder="请选择企业"
            @change="changeEnter"
          >
            <el-option
              v-for="item in zsCustomerList"
              :key="item.id"
              :label="item.enterpriseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="企业联系人" prop="enterpriseContact">
          <el-input
            disabled
            v-model="form.enterpriseContact"
            placeholder="请输入企业联系人"
          />
        </el-form-item>
        <el-form-item label="企业联系人电话" prop="contactPhone">
          <el-input
            disabled
            v-model="form.contactPhone"
            placeholder="请输入企业联系人电话"
          />
        </el-form-item>
        <el-form-item label="企业类型" prop="enterpriseType">
          <el-select
            disabled
            v-model="form.enterpriseType"
            placeholder="请选择企业类型"
          >
            <el-option
              v-for="dict in enterpriseTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业法人" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入企业法人"/>
        </el-form-item>
        <el-form-item label="法人联系电话" prop="legalPhone">
          <el-input
            v-model="form.legalPhone"
            placeholder="请输入法人联系电话"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQyEnterprise,
  getQyEnterprise,
  delQyEnterprise,
  addQyEnterprise,
  updateQyEnterprise
} from '@/api/smartParks/merchants/merchantsCompanySettlein'
import { listZsCustomer } from '@/api/smartParks/merchants/merchantsCustomer'
import { getToken } from '@/utils/auth'

export default {
  name: 'QyEnterprise',
  components: {},
  data() {
    return {

      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/energy/qyEnterprise/importData"
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      itemList: [],
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业概况表格数据
      qyEnterpriseList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 企业类型字典
      enterpriseTypeOptions: [],
      zsCustomerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        legalPerson: null,
        legalPhone: null,
        enterpriseContact: null,
        contactPhone: null,
        enterpriseType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '企业法人不能为空', trigger: 'blur' }
        ],
        legalPhone: [
          { required: true, message: '法人联系电话不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('enterprise_type').then((response) => {
      this.enterpriseTypeOptions = response.data
    })
    listZsCustomer({ settledState: 0 }).then((response) => {
      this.zsCustomerList = response.rows
    })
  },
  methods: {

    /** 下载模板操作 */
    importTemplate() {
      this.download('/energy/qyEnterprise/importTemplate',{
        ...this.queryParams
      }, `企业入驻导入模板.xlsx`)
    },

    changeEnter() {
      if (this.form.customerId) {
        let itemTep = this.zsCustomerList.filter((item) => {
          return item.id == this.form.customerId
        })
        this.form.enterpriseName = itemTep[0].enterpriseName ? itemTep[0].enterpriseName : undefined
        this.form.enterpriseContact = itemTep[0].enterpriseContact ? itemTep[0].enterpriseContact : undefined
        this.form.contactPhone = itemTep[0].contactPhone ? itemTep[0].contactPhone : undefined
        this.form.enterpriseType = itemTep[0].enterpriseType ? itemTep[0].enterpriseType : undefined
      } else {
        this.form.enterpriseName = undefined
        this.form.enterpriseContact = undefined
        this.form.contactPhone = undefined
        this.form.enterpriseType = undefined
      }

    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '入驻信息导入'
      this.upload.open = true
    },

    /** 查询企业概况列表 */
    getList() {
      this.loading = true
      listQyEnterprise(this.queryParams).then((response) => {
        this.qyEnterpriseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 企业类型字典翻译
    enterpriseTypeFormat(row, column) {
      return this.selectDictLabel(
        this.enterpriseTypeOptions,
        row.enterpriseType
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        customerId: null,
        legalPerson: null,
        legalPhone: null,
        enterpriseContact: null,
        contactPhone: null,
        enterpriseType: null,
        mainProduct: null,
        enterpriseScale: null,
        roomId: null,
        unitId: null,
        buildingId: null,
        parkId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.itemList = selection
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加企业入驻'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ name: 'QyEnterpriseAdd', query: { id: row.id } })
    },
    addApart() {
      const id = this.ids[0]
      this.$router.push({ name: 'QypartmentAdd', query: { id: id, name: this.itemList[0].enterpriseName } })
    },
    addCom() {
      const id = this.ids[0]
      this.$router.push({ name: 'QycomAdd', query: { id: id, name: this.itemList[0].enterpriseName } })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateQyEnterprise(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addQyEnterprise(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    }
    ,
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },

// 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
      this.upload.open = false

    }
  },

  /** 删除按钮操作 */
  handleDelete(row) {
    const ids = row.id || this.ids
    this.$confirm(
      '是否确认删除企业概况的数据项?',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
      .then(function() {
        return delQyEnterprise(ids)
      })
      .then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
  },
  /** 导出按钮操作 */
  handleExport() {
    this.download(
      'energy/qyEnterprise/export',
      {
        ...this.queryParams
      },
      `energy_qyEnterprise.xlsx`
    )
  },
}

</script>
